package com.xueqiu.lovepetmedicalmanagersystem.controller;

import com.xueqiu.lovepetmedicalmanagersystem.pojo.Doc;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Specialties;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.VetSpecialties;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Vets;
import com.xueqiu.lovepetmedicalmanagersystem.service.SpecialtiesService;
import com.xueqiu.lovepetmedicalmanagersystem.service.VetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private VetsService vetsService;
    @Autowired
    private SpecialtiesService specialtiesService;

    /**
     * 跳转控制器；跳转到兽医新增页面
     * @return 兽医新增页面URL
     */
    @RequestMapping("/toDocAdd")
    public String toDocAdd() {
        return "doc/docadd";
    }

    /**
     * 兽医新增
     * @param newVetData 数据源
     * @param model 模型参数
     * @return 新增成功则跳转到兽医搜索页面；新增失败则跳转到兽医新增页面
     */
    @RequestMapping("/insertDoc")
    public String insertDocAndSpe(Doc newVetData, Model model) {
        Vets originVet = vetsService.queryVetByName(newVetData.getName());
        if (originVet != null) {
            model.addAttribute("msg", "该兽医已经存在！！！");
            return "doc/docadd";
        }
        //封装兽医的新增数据源（Vets类）
        Vets newVet = new Vets();
        newVet.setName(newVetData.getName());
        newVet.setTelephone(newVetData.getTelephone());
        newVet.setAddress(newVetData.getAddress());

        //封装员工专业的新增数据源（Specialties类）
        Specialties newSpecialties = new Specialties();
        newSpecialties.setName(newVetData.getSpe());

        //查询是否存在兽医的新增数据源中的员工专业
        Specialties originSpecialties = specialtiesService.querySpecialtiesByName(newVetData.getSpe());
        //不存在，即这是个新的员工专业
        if (originSpecialties == null) {
            //先执行兽医新增
            Integer vetInsertNum = vetsService.insertVets(newVet);
            if (vetInsertNum > 0) {
                //获取兽医新增成功后的兽医ID
                Vets successVetHaveId = vetsService.queryVetByName(newVet.getName());
                //再执行员工专业新增
                Integer speInsertNum = specialtiesService.insertSpecialties(newSpecialties);
                if (speInsertNum > 0) {
                    //获取员工专业新增成功后的ID
                    Specialties successSpecialtiesHaveId = specialtiesService.querySpecialtiesByName(newSpecialties.getName());
                    //再执行兽医与员工专业绑定对象的新增
                    Integer relationshipInsertNum = vetsService.insertVetSpe(successVetHaveId.getId(), successSpecialtiesHaveId.getId());
                    if (relationshipInsertNum > 0) {
                        model.addAttribute("msg", "兽医信息添加成功！！！");
                        return "doc/docsearch";
                    } else {
                        model.addAttribute("msg", "兽医信息添加失败！！！");
                        return "doc/docadd";
                    }
                } else {
                    model.addAttribute("msg", "兽医信息添加失败！！！");
                    return "doc/docadd";
                }
            } else {
                model.addAttribute("msg", "兽医信息添加失败！！！");
                return "doc/docadd";
            }
        } else {
            //存在，即这是个老的员工专业；则先执行兽医新增
            Integer vetInsertNum = vetsService.insertVets(newVet);
            if (vetInsertNum > 0) {
                //获取兽医新增成功后的兽医ID
                Vets successVetHaveId = vetsService.queryVetByName(newVet.getName());
                //再执行兽医与员工专业绑定对象新增
                Integer relationshipInsertNum = vetsService.insertVetSpe(successVetHaveId.getId(), originSpecialties.getId());
                if (relationshipInsertNum > 0) {
                    model.addAttribute("msg", "兽医信息添加成功！！！");
                    return "doc/docsearch";
                } else {
                    model.addAttribute("msg", "兽医信息添加失败！！！");
                    return "doc/docadd";
                }
            } else {
                model.addAttribute("msg", "兽医信息添加失败！！！");
                return "doc/docadd";
            }
        }
    }

    /**
     * 兽医更新
     * @param vetId 兽医ID
     * @param model 模型参数
     * @return 查询成功则跳转到兽医更新页面；查询失败则跳转到兽医搜索页面
     */
    @RequestMapping("/toDocUpdate")
    public String toDocUpdate(Integer vetId, Model model) {
        Vets vets = vetsService.queryVetById(vetId);
        if (vets != null) {
            VetSpecialties vetSpecialties = vetsService.queryVetSpecialtiesByVetId(vets.getId());
            Specialties specialties = specialtiesService.querySpecialtiesById(vetSpecialties.getSpecialty());
            List<Specialties> speList = specialtiesService.getAllSpe();

            model.addAttribute("vets", vets);
            model.addAttribute("speList", speList);
            model.addAttribute("vetSpecialties", vetSpecialties);
            model.addAttribute("specialties", specialties);
            return "doc/docupdate";
        } else {
            model.addAttribute("msg", "查找失败，请输入正确的兽医ID！");
            return "doc/docsearch";
        }
    }

    /**
     * 兽医更新，包括绑定关系更新
     * @param vet 数据源
     * @param model 模型参数
     * @return 更新成功则跳转到兽医搜索页面；更新失败则跳转到兽医更新页面
     */
    @RequestMapping("/updateDoc")
    public String updateDoc(Doc vet, Model model) {
        //封装兽医更新用数据源
        Vets vets = new Vets();
        vets.setId(vet.getId());
        vets.setName(vet.getName());
        vets.setTelephone(vet.getTelephone());
        vets.setAddress(vet.getAddress());
        //更新兽医
        Integer i = vetsService.updateVets(vets);
        //更新绑定关系
        Specialties specialties = specialtiesService.querySpecialtiesByName(vet.getSpe());
        Integer i1 = vetsService.updateVetSpe(vets.getId(), specialties.getId());

        if (i > 0 && i1 > 0) {
            model.addAttribute("msg", "修改成功！！！");
            return "doc/docsearch";
        } else {
            model.addAttribute("msg", "修改失败！！！");
            return "doc/docupdate";
        }
    }

    /**
     * 跳转控制器；跳转到兽医搜索页面
     * @return 兽医搜索页面URL
     */
    @RequestMapping("/toDocSearch")
    public String toDocSearch() {
        return "doc/docsearch";
    }

    /**
     * 查询兽医
     * @param vetName 兽医姓名
     * @param speName 员工专业名称
     * @param model 模型参数
     * @return 查询成功则跳转到兽医搜索结果页面，查询成功可分为兽医名字查询成功和专业名称查询成功；查询失败则跳转到兽医搜索页面
     */
    @RequestMapping("/DocSearch")
    public String docSearch(String vetName, String speName, Model model) {
        if (!StringUtils.isEmpty(vetName)) {
            Vets vets = vetsService.queryVetByName(vetName);
            System.out.println(vets);
            if (vets != null) {
                VetSpecialties vetSpecialties = vetsService.queryVetSpecialtiesByVetId(vets.getId());
                Specialties specialties = specialtiesService.querySpecialtiesById(vetSpecialties.getSpecialty());
                model.addAttribute("vets", vets);
                model.addAttribute("vetSpecialties", vetSpecialties);
                model.addAttribute("specialties", specialties);
                model.addAttribute("qq", "1");

                return "doc/docresult";
            } else {
                model.addAttribute("msg", "查找失败，请输入正确的名称！");
                return "doc/docsearch";
            }
        } else if (!StringUtils.isEmpty(speName)) {
            Specialties specialties = specialtiesService.querySpecialtiesByName(speName);
            if (specialties != null) {
                List<VetSpecialties> vetSpecialties = specialtiesService.queryVetSpecialtiesBySpeId(specialties.getId());
                List<Vets> vetsList = new ArrayList<>();
                for (VetSpecialties vetSpecialty : vetSpecialties) {
                    Vets vet = specialtiesService.queryVets(vetSpecialty.getVetId());
                    vetsList.add(vet);
                }
                model.addAttribute("vetsList", vetsList);
                model.addAttribute("specialties", specialties);
                model.addAttribute("qq", "2");
                return "doc/docresult";
            } else {
                model.addAttribute("msg", "查找失败，请输入正确的专业！");
                return "doc/docsearch";
            }
        }
        model.addAttribute("msg", "查找失败！！！");
        return "doc/docsearch";
    }

    /**
     * 兽医删除
     * @param vetId 兽医ID
     * @param model 模型参数
     * @return 删除成功则跳转到兽医搜索页面；删除失败则跳转到兽医更新页面
     */
    @RequestMapping("/deleteDoc")
    public String deleteDoc(Integer vetId, Model model) {
        Integer i = vetsService.deleteVetsById(vetId);
        Integer i1 = vetsService.deleteVetsSpeById(vetId);

        if (i > 0 && i1 > 0) {
            model.addAttribute("msg", "删除成功！！！");
            return "doc/docsearch";
        } else {
            model.addAttribute("msg", "删除失败！！！");
            return "doc/docupdate";
        }
    }
}
